package com.example.ximalaya.presenters;

import android.util.Log;

import com.example.ximalaya.interfaces.IRecommendPresenter;
import com.example.ximalaya.interfaces.IRecommendViewCallback;
import com.example.ximalaya.utils.Constant;
import com.example.ximalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG="RecommendPresenter";
    private List<IRecommendViewCallback> mCallbacks=new ArrayList<>();

    private RecommendPresenter(){

    }
    private static RecommendPresenter sInstance=null;

    /*
    获取单例对象
     */
    public static RecommendPresenter getInstance(){
        if(sInstance==null){
            synchronized (RecommendPresenter.class){
                if(sInstance==null){
                    sInstance=new RecommendPresenter();
                }
            }
        }
        return sInstance;
    }

    /*
       获取推荐内容，其实就是猜你喜欢
       这个接口 3.10.6 获取猜你喜欢专辑
    */
    @Override
    public void getRecommendList() {
        //获取推荐内容
        //封装参数
        Map<String, String> map = new HashMap<>();
        //这个参数表示一页数据返回多少条
        map.put(DTransferConstants.LIKE_COUNT, Constant.RECOMMEND_COUNT+"");
        //通过验证下面方法是在主线程中完成
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                //数据获取成功
                if(gussLikeAlbumList!=null){
                    List<Album> albumList = gussLikeAlbumList.getAlbumList();
                  /*  if(albumList!=null){
                        LogUtil.d(TAG,"size---->"+albumList.size());
                    }*/
                    for(int i=0;i<albumList.size();i++){
                        LogUtil.d(TAG,albumList.get(i)+"");
                    }
                    //数据回来后，我们去更新UI
                    //upRecommendUI(albumList);
                    handleRecommendResult(albumList);
                }
            }

            @Override
            public void onError(int i, String s) {
                Log.d(TAG, "onError: "+Thread.currentThread().getName());
                //数据获取失败
                LogUtil.d(TAG,"error---->"+i);
                LogUtil.d(TAG,"errorMsg----->"+s);

            }
        });

    }

    private void handleRecommendResult(List<Album> albumList) {
        //通知UI更新
        if(mCallbacks!=null){
            for (IRecommendViewCallback callback:mCallbacks){
                callback.onRecommendListLoaded(albumList);
            }
        }

    }

    @Override
    public void pullRefreshMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void registerViewCallback(IRecommendViewCallback callback) {

        if(mCallbacks!=null&&!mCallbacks.contains(callback)){
            mCallbacks.add(callback);
        }
    }

    @Override
    public void unRegisterViewCallback(IRecommendViewCallback callback) {
           if(mCallbacks!=null){
               mCallbacks.remove(mCallbacks);
           }
    }
}
